//
//  MSSetUpController.m
//  美娱
//
//  Created by No.8 on 2019/4/9.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSSetUpController.h"
#import "MSSetUpCell.h"
#import "MSAccuntLoginController.h"
#import "MSAboutMeController.h"
#define AppID

@interface MSSetUpController ()<UITableViewDelegate,UITableViewDataSource>
//通用导航栏
{
    CustomNavigationBar *naviagtionBar;
    
}
/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;


/** 缓存大小*/
@property(nonatomic,strong)NSString *sizeStr;

@end

@implementation MSSetUpController

/** Nav实现 */
- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createNavigationBar];
    
    
    [self CreattableView];
    
    
    /** 退出登录*/
    UIButton * backButton = [[UIButton alloc] init];
    backButton.frame = CGRectMake(100 * px, ScreenHeight - 200 * px, ScreenWidth - 200 * px, 120 * px);
    backButton.layer.cornerRadius = 60 * px;
    backButton.layer.masksToBounds = YES;
    backButton.layer.borderColor = BackViewColor.CGColor;
    backButton.layer.borderWidth = 1;
    [backButton setTitle:@"退出登录" forState:UIControlStateNormal];
    backButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [backButton  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backButton  addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view  addSubview:backButton];
    
    
}


/** 退出登录*/
- (void)backButtonClick{
    
    //第一步:创建控制器
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"确定要退出嘛?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //第二步:创建按钮
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");
    }];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
                
        [WYCIsAppLoginTool clearIsAppLogin];
        
        NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
        
        [userDefaults removeObjectForKey:@"account"];
        [userDefaults removeObjectForKey:@"pwd"];
        
        [userDefaults synchronize];
        
        
        MSAccuntLoginController * login = [[MSAccuntLoginController alloc] init];
        [self presentViewController:login animated:NO completion:nil];
        
    }];
    //第三步:添加按钮
    [alertVC addAction:action];
    [alertVC addAction:action1];
    //第四步:显示弹框.(相当于show操作)'
    [self presentViewController:alertVC animated:YES completion:nil];
}
/** 表视图*/
- (void)CreattableView{
    if (@available(iOS 11.0, *)) {
        
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
    } else {
        
        self.automaticallyAdjustsScrollViewInsets = NO;
        
    }
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, [self navBarBottom], ScreenWidth, 290 * px) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
    //    }
    //    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140 * px - 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MSSetUpCell * cell = [MSSetUpCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    //无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.row == 0) {
        
        cell.IMGName.image = [UIImage imageNamed:@"gywm_icon"];
        cell.TitleName.text = @"关于我们";
       
    }else{
        cell.IMGName.image = [UIImage imageNamed:@"jcgx_icon"];
        cell.TitleName.text = @"检查更新";
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        MSAboutMeController * aboutVC = [[MSAboutMeController alloc] init];
        [self.navigationController pushViewController:aboutVC animated:NO];
    }else{
//        NSString * url = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@",AppID];//替换为自己App的ID
//        // 获取本地版本号
//        NSString * currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
//        // 网络请求获取最新版本
//        [[HttpBaseManager getSharedManager] GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            NSArray * results = responseObject[@"results"];
//            if (results && results.count>0)
//            {
//                NSDictionary * dic = results.firstObject;
//                NSString * lineVersion = dic[@"version"];//版本号
//                NSString * releaseNotes = dic[@"releaseNotes"];//更新说明
//                //NSString * trackViewUrl = dic[@"trackViewUrl"];//链接
//                //把版本号转换成数值
//                NSArray * array1 = [currentVersion componentsSeparatedByString:@"."];
//                NSInteger currentVersionInt = 0;
//                if (array1.count == 3)//默认版本号1.0.0类型
//                {
//                    currentVersionInt = [array1[0] integerValue]*100 + [array1[1] integerValue]*10 + [array1[2] integerValue];
//                }
//                NSArray * array2 = [lineVersion componentsSeparatedByString:@"."];
//                NSInteger lineVersionInt = 0;
//                if (array2.count == 3)
//                {
//                    lineVersionInt = [array2[0] integerValue]*100 + [array2[1] integerValue]*10 + [array2[2] integerValue];
//                }
//                if (lineVersionInt > currentVersionInt)//线上版本大于本地版本
//                {
//                    UIAlertController * alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"发现新版本%@",lineVersion] message:releaseNotes preferredStyle:UIAlertControllerStyleAlert];
//                    UIAlertAction * ok = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
//                    UIAlertAction * update = [UIAlertAction actionWithTitle:@"去更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                        //跳转到App Store
//                        NSString *urlStr = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8",AppID];
//                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
//                    }];
//                    [alert addAction:ok];
//                    [alert addAction:update];
//                    [self presentViewController:alert animated:YES completion:nil];
//                }
//            }
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            NSLog(@"版本更新出错，%@",error.description);
//        }];

    }
}


- (void)createNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"设置";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}



- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}

@end
